﻿using System.Linq;
using PresentatieSite.Data;

namespace PresentatieSite.Core.Repositories
{
    class ShopRepository
    {
        //Database object
        private readonly PresentatieDBDataContext _context = new PresentatieDBDataContext();

        public IQueryable<Models.Product> ProductRepo
        {
            get
            {
                return _context.Products.Select(x => new Models.Product
                {
                    ProductId = x.ID,
                    ProductName = x.Name,
                    ProductDescription = x.Description,
                    ProductPrice = x.Price,
                    ProductImage = x.Image,
                    ProductStart = x.Start
                });
            }
        }

        public void InsertProduct(Models.Product product)
        {
            Product productToInsert = new Product
            {
                ProductName = product.ProductName,
                ProductDescription = product.ProductDescription,
                ProductPrice = product.ProductPrice,
                ProductImage = product.ProductImage,
                ProductStart = product.ProductStart
            };

            _context.Products.InsertOnSubmit(productToInsert);
            _context.SubmitChanges();
        }
        public void UpdateProduct(Models.Product product)
        {
            Product productToUpdate = _context.Products.First(x => x.ID.Equals(product.ProductId));
            productToUpdate.ProductName = product.ProductName;
            productToUpdate.ProductDescription = product.ProductDescription;
            productToUpdate.ProductPrice = product.ProductPrice;
            productToUpdate.ProductImage = product.ProductImage;
            productToUpdate.Start = product.ProductStart;

            _context.SubmitChanges();
        }
        public void RemoveProduct(Models.Product product)
        {
            Product productToRemove = _context.Products.First(x => x.ID.Equals(product.ProductId));
            _context.Products.DeleteOnSubmit(productToRemove);
            _context.SubmitChanges();
        }
    }
}
